package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.commons.ex.ServiceException;
import cn.tedu.csmall.commons.web.ServiceCode;
import cn.tedu.csmall.product.mapper.AlbumMapper;
import cn.tedu.csmall.product.mapper.PictureMapper;
import cn.tedu.csmall.product.mapper.SkuMapper;
import cn.tedu.csmall.product.mapper.SpuMapper;
import cn.tedu.csmall.product.pojo.entity.Album;
import cn.tedu.csmall.product.pojo.entity.Picture;
import cn.tedu.csmall.product.pojo.entity.Sku;
import cn.tedu.csmall.product.pojo.entity.Spu;
import cn.tedu.csmall.product.pojo.param.AlbumAddNewParam;
import cn.tedu.csmall.product.pojo.param.AlbumUpdateInfoParam;
import cn.tedu.csmall.product.pojo.vo.AlbumListItemVo;
import cn.tedu.csmall.product.pojo.vo.AlbumStandardVo;
import cn.tedu.csmall.commons.pojo.vo.PageData;
import cn.tedu.csmall.product.repository.AlbumCacheRepository;
import cn.tedu.csmall.product.service.AlbumService;
import cn.tedu.csmall.commons.util.PageInfoToPageDataConverter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j  /*要养成写日志的习惯*/
@Service
public class AlbumServiceImpl implements AlbumService {

    @Autowired
    private AlbumMapper albumMapper;
    @Autowired
    private PictureMapper pictureMapper;
    @Autowired
    private SkuMapper skuMapper;
    @Autowired
    private SpuMapper spuMapper;
    @Autowired
    AlbumCacheRepository albumCacheRepository;

    @Override
    public void addNew(AlbumAddNewParam albumAddNewParam) {
        log.debug("开始处理【添加相册】的业务,参数: {}",albumAddNewParam);
        //检查相册名称是否被占用，如果被占用，则抛出异常
        QueryWrapper<Album> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",albumAddNewParam.getName());
        int countByName = albumMapper.selectCount(queryWrapper);
        log.debug("根据相册名称统计匹配的相册数量，结果：{}", countByName);
        if (countByName>0){
            String message = "添加相册失败，相册名称已经被占用";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        // 将相册数据写入到数据库中
        Album album = new Album();
        BeanUtils.copyProperties(albumAddNewParam,album);
        album.setGmtCreate(LocalDateTime.now());
        album.setGmtModified(LocalDateTime.now());
        log.debug("准备将新的相册数据写入到数据库，数据：{}", album);
        int rows = albumMapper.insert(album);
        if (rows != 1){
            String message = "添加管理员失败，服务器忙，请稍后再试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
        log.debug("将新的相册数据写入到数据库，完成！");
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【根据ID删除相册】的业务,参数: {}",id);
        //检查相册是否存在，如果不存在，则抛出异常
        QueryWrapper<Album> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        int countById = albumMapper.selectCount(queryWrapper);
        log.debug("根据相册ID统计匹配的相册数量，结果：{}", countById);
        if (countById == 0){
            String message = "删除相册失败，相册数据不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }

        //检查是否有图片关联到此相册，如果存在，则抛出异常
        QueryWrapper<Picture> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("album_id",id);
        int countByAlbumId = pictureMapper.selectCount(queryWrapper2);
        log.debug("根据相册名称统计匹配的图片数量，结果：{}", countByAlbumId);
        if (countByAlbumId > 0){
            String message = "删除相册失败，仍有图片关联到此相册";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        //检查是否有sku关联到此相册，如果存在，则抛出异常
        QueryWrapper<Sku> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.eq("album_id",id);
        int countByAlbumId3 = skuMapper.selectCount(queryWrapper3);
        log.debug("根据相册名称统计匹配的sku数量，结果：{}", countByAlbumId3);
        if (countByAlbumId3 > 0){
            String message = "删除相册失败，仍有sku关联到此相册";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        //检查是否有spu关联到此相册，如果存在，则抛出异常
        QueryWrapper<Spu> queryWrapper4 = new QueryWrapper<>();
        queryWrapper4.eq("album_id",id);
        int countByAlbumId4 = spuMapper.selectCount(queryWrapper4);
        log.debug("根据相册名称统计匹配的spu数量，结果：{}", countByAlbumId4);
        if (countByAlbumId4 > 0){
            String message = "删除相册失败，仍有spu关联到此相册";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        int rows = albumMapper.deleteById(id);
        if (rows != 1){
            String message = "删除相册失败，服务器忙，请稍后再试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
        log.debug("删除相册成功！");
    }

    @Override
    public void updateInfoById(Long id, AlbumUpdateInfoParam albumUpdateInfoParam) {
        log.debug("开始处理【根据ID修改相册】的业务，ID:{},新数据:{}",id,albumUpdateInfoParam);
        QueryWrapper<Album> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        int countById = albumMapper.selectCount(queryWrapper);
        log.debug("根据相册ID统计匹配的相册数量，结果：{}", countById);
        if (countById == 0){
            String message = "修改相册详情失败，相册数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }

        //检查相册名称是否被其他相册占用，如果被占用，则抛出异常
        QueryWrapper<Album> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("name",albumUpdateInfoParam.getName())
                .ne("id",id);
        // where name=? and id<>?
        // where name='Redmi Note 15 5G的相册' and id<>1   >>> 0
        // where name='华为P50的相册' and id<>1   >>> 1
        int countByName = albumMapper.selectCount(queryWrapper2);
        log.debug("根据相册名称统计匹配的相册数量，结果：{}", countByName);
        if (countByName > 0) {
            String message = "修改相册详情失败，相册名称已经被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        //执行修改
        Album album = new Album();
        BeanUtils.copyProperties(albumUpdateInfoParam,album);
        album.setId(id);
        album.setGmtModified(LocalDateTime.now());
        int rows = albumMapper.updateById(album);
        if (rows != 1){
            String message = "修改相册失败，服务器忙，请稍后再试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
        log.debug("修改相册成功!");
    }

    @Override
    public PageData<AlbumListItemVo> list(Integer pageNum) {
        Integer pageSize = 5;
        return list(pageNum,pageSize);
    }

    @Override
    public PageData<AlbumListItemVo> list(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询相册列表】的业务，页码:{},每页记录数:{}",pageNum,pageSize);
        PageHelper.startPage(pageNum,pageSize);
        List<AlbumListItemVo> list = albumMapper.list();
        PageInfo<AlbumListItemVo> pageInfo = new PageInfo<>(list);
        PageData<AlbumListItemVo> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        log.debug("查询完成，即将返回:{}",pageData);
        return pageData;
    }

    @Override
    public AlbumStandardVo getStandardById(Long id) {
        log.debug("开始处理【根据ID查询相册详情】的业务，参数:{}",id);
        AlbumStandardVo queryResult = albumCacheRepository.getStandardById(id);
        if (queryResult == null) {
            AlbumStandardVo standardById = albumMapper.getStandardById(id);
            if (standardById == null){
                String message = "查询相册详情失败，相册数据不存在";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
            }
            albumCacheRepository.save(standardById);
            return standardById;
        }
        return queryResult;
    }
}
